<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
 
 	function _listuser(){
    	$this->load->database();
    	$this->db->order_by("id", "desc");  
        $query=$this->db->get('tbuser');
        if ($query->num_rows() > 0)
        {
          return $query->result();
        } 
        return $query->result();
    }
    
    function checkuserexit($username){
    	$this->load->database(); 
		$this->db->where(array('username'=>$username));
		$query = $this->db->get('tbuser');
		if($query->num_rows() > 0){
			foreach($query->result() as $row){
				return $row->id; 
				 
			}
		}else{
			return 0;
		}
    }
    
	function _adduser($username,$userpass,$phone,$email,$ip){
    	$this->load->database();
    	if($this->checkuserexit($username) == 0){
	    	$data = array( 
	            'username'=>strtolower($username),
	            'password'=>md5(strtolower($userpass)),
	    		'phone'=>$phone,
	    		'createdate'=>date("Y-m-d h:s:m"),
	    		'active'=>1,
	    		'email'=>$email,
	    		'registerIP'=>$ip
	            );
	    	$this->db->insert('tbuser', $data);
	    	return 1;
    	} else {
    		return 0;
    	}
    }
    
    function _updateuserinfo($userid,$fullname,$bankname,$bankaccount,$note){ 
    	$this->load->database();
        $data = array( 
            'fullname'=>$fullname,
            'bankname'=>$bankname,
            'accbank'=>$bankaccount, 
        	'note'=>$note
            );
        $this->db->where('id', $userid);
        $this->db->update('tbuser', $data); 
    }
	
    function checkuser($username,$password){
		$this->load->database(); 
		$this->db->where(array('username'=>$username,'password'=>$password));
		$query = $this->db->get('tbuser');
		if($query->num_rows() > 0){
			foreach($query->result() as $row){
				return $row->id; 
			}
		}else{
			return null;
		}
	}
	
	function listalluser($num,$offset){
		$this->load->database();
		$this->db->order_by("id", "desc"); 
		$query=$this->db->get('tbuser',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	} 
	function totaluser(){  
    	$this->load->database();
		return $this->db->count_all('tbuser'); 
	}
	function showaccount($userid){
	$this->load->database(); 
		$this->db->where('id',$userid);
		$query = $this->db->get('tbuser');
		if($query->num_rows() > 0){
			 return $query->result();
		}else{
			return null;
		}
	}
}
    ?>